package com.foxmail.aroundme.boring.manager;

import android.content.Context;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.foxmail.aroundme.boring.BuildConfig;
import com.foxmail.aroundme.boring.utils.helper.BaseInfoHelper;
import com.socks.library.KLog;

import java.util.UUID;

/**
 * Created by lianzhan on 16/5/4.
 */
public class SettingManager {
    private static SettingManager sInstance;
    private static final Integer sLockInstance = new Integer(0);

    private int mTextSize;
    private boolean isLogin;
    private long mVersionCode = 0;
    private boolean isNonImage = false;
    private boolean isLowDialogShow = false;
    private String mSession = "";
    private String mUrl = "";
    private int w = 0;
    private int h = 0;
    private float density = 0;
    private int mWebViewW = 0;
    //菜单是否展开
    private boolean isMenuShow = false;
    private int pullRefreshCount = 0;
    private boolean isExit = true;
    private int mOperaVersion;
    private String mCachePath = "";
    private String mUserId = "";
    private String mAbFlag = "";

    /**
     * 获取唯一实例
     *
     * @return SettingManager 设置管理对象
     */
    public static SettingManager getInstance() {
        if (sInstance == null) {
            synchronized (sLockInstance) {
                if (sInstance == null) {
                    sInstance = new SettingManager();
                }
            }
        }

        return sInstance;
    }



    //是否登录
    public boolean isLogin() {
        return isLogin;
    }

    public boolean isNonImage() {
        return isNonImage;
    }


    private void setVersionCode(long versionCode) {
        mVersionCode = versionCode;
    }

    public boolean isLowDialogShow() {
        return isLowDialogShow;
    }


    //获取SESSION
    public String getSession() {
        if (TextUtils.isEmpty(mSession)) {
            mSession = UUID.randomUUID().toString();
            setSession(mSession);
        }
        if (BuildConfig.DEBUG) {
            KLog.i("返回session = " + mSession);
        }
        return mSession;
    }

    //创建最新session
    public void crateNewSession() {
        mSession = UUID.randomUUID().toString();
        setSession(mSession);
    }

    //保存最新session
    private void setSession(String session) {
        mSession = session;
        if (BuildConfig.DEBUG) {
            KLog.i("保存session = " + session);
        }
    }

    public String getUpdateUrl() {
        return mUrl;
    }

    private void setUpdateUrl(String url) {
        mUrl = url;
    }

    public boolean isMenuShow() {
        return isMenuShow;
    }

    public void setIsMenuShow(boolean isMenuShow) {
        this.isMenuShow = isMenuShow;
    }




    public int getW(Context context) {
        if (w <= 0) {
            DisplayMetrics displayMetrics = BaseInfoHelper.getDisplayMetrics(context);
            w = displayMetrics.widthPixels;
        }
        return w;
    }


    public int getH(Context context) {
        if (h <= 0) {
            DisplayMetrics displayMetrics = BaseInfoHelper.getDisplayMetrics(context);
            h = displayMetrics.heightPixels;
        }
        return h;
    }



    public int getPullRefreshCount() {
        return pullRefreshCount;
    }

    public void setPullRefreshCount(int pullRefreshCount) {
        this.pullRefreshCount = pullRefreshCount;
    }

    public boolean isExit() {
        return isExit;
    }

    public void setIsExit(boolean isExit) {
        this.isExit = isExit;
    }



    public int getOperaVersion() {
        return mOperaVersion;
    }

    public void setOperaVersion(int mOperaVersion) {
        this.mOperaVersion = mOperaVersion;
    }



    //用于小流量测试使用
    public String getAbFlag() {
        return TextUtils.isEmpty(mAbFlag) ? "" : mAbFlag;
    }

    //设置小流量测试
    public void setAbFlag(String abFlag) {
        this.mAbFlag = abFlag;
    }
}
